/**
 * @file
 * @author  Tsin Smith <tsin.creator@gmail.com>
 *
 * @section DESCRIPTION
 *
 */

#ifndef __TSINIDE_CODEEDITOR_HPP__
#define __TSINIDE_CODEEDITOR_HPP__

#include <QPlainTextEdit>
#include <QSyntaxHighlighter>

class Highlighter : public QSyntaxHighlighter
{
        Q_OBJECT

public:
        Highlighter(QTextDocument *parent = 0);

protected:
        void highlightBlock(const QString &text);

private:
        enum LastBlockCommentState {
                IN_COMMENT = 1,
                NOT_IN_COMMENT = 2
        };

        struct HighlightingRule
        {
                QRegExp pattern;
                QTextCharFormat format;
        };
        QVector<HighlightingRule> highlightingRules;

        QRegExp commentStartExpression;
        QRegExp commentEndExpression;

        QTextCharFormat normalFormat;
        QTextCharFormat headerFormat;
        QTextCharFormat keywordFormat;
        QTextCharFormat numberFormat;
        QTextCharFormat typeFormat;
        QTextCharFormat commentFormat;
};

class CodeEditor : public QPlainTextEdit
{
        Q_OBJECT

public:
        CodeEditor(QWidget *parent = 0);
        ~CodeEditor();

        void lineNumberAreaPaintEvent(QPaintEvent *event);
        int lineNumberAreaWidth();

protected:
        void resizeEvent(QResizeEvent *event);

private slots:
        void updateLineNumberAreaWidth(int newBlockCount);
        void highlightCurrentLine();
        void updateLineNumberArea(const QRect &, int);

private:
        QWidget *lineNumberArea;
        Highlighter *highlighter;
};

#endif /* __TSINIDE_CODEEDITOR_HPP__ */
